package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpCardRequireDAO;
import org.jsola.hr.dto.EmpCardRequireAddDTO;
import org.jsola.hr.dto.EmpCardRequireUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpCardRequireDO;
import org.jsola.hr.query.EmpCardRequireQuery;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IEmpCardRequireService;
import org.jsola.hr.vo.EmpCardRequireListVO;
import org.jsola.hr.vo.EmpCardRequireVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrEmpCardRequireServiceImpl")
public class EmpCardRequireServiceImpl implements IEmpCardRequireService {

    @Autowired
    private IEmpCardRequireDAO empCardRequireDAO;

    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpCardRequireVO save(EmpCardRequireAddDTO empCardRequireAddDTO, TokenUser tokenUser) {
        // 校验是否存在
        EmpCardRequireVO requireVO = findByCompanyAndType(
                empCardRequireAddDTO.getCompanyId(),
                empCardRequireAddDTO.getRequireType(),
                tokenUser.getSiteId());
        if (!Objects.isNull(requireVO)) {
            // 修改
            EmpCardRequireUpdateDTO requireUpdateDTO = empCardRequireAddDTO.to(EmpCardRequireUpdateDTO.class);
            requireUpdateDTO.setId(requireVO.getId());

            update(requireUpdateDTO, tokenUser);
            return selectById(requireVO.getId(), tokenUser.getSiteId());
        }
        // 转DO
        EmpCardRequireDO empCardRequireDO = empCardRequireAddDTO.to(EmpCardRequireDO.class);
        // 保存
        empCardRequireDO = save(empCardRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empCardRequireDO.to(EmpCardRequireVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpCardRequireUpdateDTO empCardRequireUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpCardRequireDO empCardRequireDO = empCardRequireUpdateDTO.to(EmpCardRequireDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empCardRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empCardRequireIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empCardRequireIds);
    }


    @Override
    public EmpCardRequireVO selectById(String empCardRequireId, String siteId) {
        EmpCardRequireDO empCardRequireDO = selectDOById(empCardRequireId, siteId);
        if (empCardRequireDO == null) {
            return null;
        }
        return empCardRequireDO.to(EmpCardRequireVO.class);
    }

    @Override
    public List<EmpCardRequireListVO> select(EmpCardRequireQuery empCardRequireQuery, String siteId) {
        List<EmpCardRequireDO> empCardRequireDOList = selectDO(empCardRequireQuery, siteId);
        if(CollectionUtils.isEmpty(empCardRequireDOList)) {
            return empCardRequireDOList == null ? null : new ArrayList<>();
        }
        return empCardRequireDOList.stream()
                .map(empCardRequireDO -> empCardRequireDO.to(EmpCardRequireListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpCardRequireQuery empCardRequireQuery, String siteId) {
        Example example = buildExample(empCardRequireQuery,siteId);
        return empCardRequireDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpCardRequireListVO> selectPage(EmpCardRequireQuery empCardRequireQuery, String siteId) {
        Example example = buildExample(empCardRequireQuery,siteId);
        Page<EmpCardRequireDO> page = empCardRequireDAO.selectPageByExample(example,
                empCardRequireQuery.getPageNo(),
                empCardRequireQuery.getPageSize());

        return page.to(EmpCardRequireListVO.class);
    }

    @Override
    public EmpCardRequireDO selectDOById(String empCardRequireId, String siteId) {
        return listById(empCardRequireId, siteId);
    }

    @Override
    public List<EmpCardRequireDO> selectDO(EmpCardRequireQuery empCardRequireQuery, String siteId) {
        Example example = buildExample(empCardRequireQuery,siteId);
        return empCardRequireDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpCardRequireVO selectByCompanyAndType(String companyId, String requireType, TokenUser tokenUser) {
        EmpCardRequireVO requireVO = findByCompanyAndType(companyId, requireType, tokenUser.getSiteId());
        if (Objects.isNull(requireVO)) {
            CompanyDO company = companyService.selectDOById(companyId, tokenUser.getSiteId());
            if (Objects.isNull(company)) {
                return null;
            }
            // 没有初始化一条
            EmpCardRequireAddDTO requireAddDTO = new EmpCardRequireAddDTO();
            requireAddDTO.setCompanyId(companyId);
            requireAddDTO.setRequireType(requireType);

            requireVO = save(requireAddDTO, tokenUser);
        }
        return requireVO;
    }

    /**
     * 根据公司id和类型查询所需信息
     *
     * @param companyId   公司id
     * @param requireType 类型
     * @param siteId      站点
     * @return 所需信息
     */
    private EmpCardRequireVO findByCompanyAndType(String companyId, String requireType, String siteId) {
        Example example = new Example(EmpCardRequireDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("requireType", requireType)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        EmpCardRequireDO requireDO = empCardRequireDAO.selectOneByExample(example);
        if (Objects.isNull(requireDO)) {
            return null;
        }
        return requireDO.to(EmpCardRequireVO.class);
    }


    /**
     * 根据查询参数，构建example

     * @param empCardRequireQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpCardRequireQuery empCardRequireQuery, String siteId) {
        Example example = new Example(EmpCardRequireDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empCardRequireQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empCardRequireQuery.getCompanyId());
        }
        if (empCardRequireQuery.getRequireType() != null) {
            example.and().andEqualTo("requireType", empCardRequireQuery.getRequireType());
        }
        if (empCardRequireQuery.getType() != null) {
            example.and().andEqualTo("type", empCardRequireQuery.getType());
        }
        if (empCardRequireQuery.getNumber() != null) {
            example.and().andEqualTo("number", empCardRequireQuery.getNumber());
        }
        if (empCardRequireQuery.getPhoto() != null) {
            example.and().andEqualTo("photo", empCardRequireQuery.getPhoto());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empCardRequireQuery.getOrders());
        return example;
    }
}




